package it.univpm.diiga.memorysim.data;

import it.univpm.diiga.memorysim.kernel.shared.EventsDisk;
import it.univpm.diiga.memorysim.kernel.shared.Kernel;
import it.univpm.diiga.memorysim.kernel.shared.ProcessRequest;
import it.univpm.diiga.memorysim.kernel.shared.SimEvent;
import java.util.ArrayList;
import java.util.Hashtable;

//Vector <Situation> fine= kernel.getSimulation(1,1,1,,1,1);

public class Situation {
	
	private static ArrayList<SimEvent> events=new ArrayList<SimEvent>();
	//----------------------------------------------------------------//
	private static ArrayList<SimEvent> HDDevents=new ArrayList<SimEvent>();
	private static Hashtable<Integer, ArrayList<Integer>> HDDstate = new Hashtable<Integer, ArrayList<Integer>>();
	private static Hashtable<Integer, ArrayList<String>> HDDlistFiles = new Hashtable<Integer, ArrayList<String>>();
	
	/*
	public static void getDummyResponse(){
		// Stato della memoria
		 Hashtable<Integer, ProcessRequest> InitMem;
		 Hashtable<Integer, ProcessRequest> InitSwap;
		InitMem=new Hashtable<Integer, ProcessRequest>();
		InitMem.put(0, new ProcessRequest(1,1));
		InitMem.put(1, new ProcessRequest(1,2));
		InitMem.put(2, new ProcessRequest(2,1));
		InitMem.put(3, new ProcessRequest(3,2));
		Hashtable<Integer, ProcessRequest> InitMem1 = new Hashtable<Integer, ProcessRequest>();
		InitMem1.put(0, new ProcessRequest(1,1));
		InitMem1.put(1, new ProcessRequest(1,2));
		InitMem1.put(2, new ProcessRequest(2,1));
		InitMem1.put(3, new ProcessRequest(3,2));
		InitMem1.put(4, new ProcessRequest(3,3));
		InitMem1.put(5, new ProcessRequest(3,4));
		InitSwap=new Hashtable<Integer, ProcessRequest>();
		InitSwap.put(0, new ProcessRequest(3,1));
		InitSwap.put(1, new ProcessRequest(1,3));
		//la prima riga deflla tabella è la situazione iniziale della memoria centrale
		events=new ArrayList<SimEvent>();
		events.add(new SimEvent(1,"GET",1,InitMem,InitSwap,0));  //situazione iniziale memoria centrale
		events.add(new SimEvent(1,"Page Fault",1,3,InitMem,InitSwap,0));
		events.add(new SimEvent(2,"GET",1,InitMem,InitSwap,0));
		events.add(new SimEvent(1,"GET",2,InitMem,InitSwap,0));
		events.add(new SimEvent(3,"Page Fault",2,1,InitMem1,InitSwap,0));
	}
	
	public static void createTableFile(){
		//tabella eventi
		HDDevents=new ArrayList<SimEvent>();
		HDDevents.add(new SimEvent("Prova","/Disco/prova","Lettura","0"));  
		HDDevents.add(new SimEvent("Prova","/Disco/prova/1","Scrittura","2")); 
		HDDevents.add(new SimEvent("Prova","/asdasdasd/prova/1","Scrittura","3")); 
	}
	
	public static void createOutputHDD(){
		//uno stato dell'HDD
		//al caricamento tenere conto che \per valori inferiori al Mega si considera valore 1.
		
		//prima riga della tabella
		ArrayList<Integer> HDDarea=new ArrayList<Integer>(); // MByte
		ArrayList<String> HDDfiles=new ArrayList<String>();  //path, se -1 vuoto
		HDDarea.add(2000);
		HDDfiles.add("/Disco/prova2/1");
		HDDarea.add(3000);
		HDDfiles.add("/Disco/prova");
		HDDarea.add(6000);
		HDDfiles.add("/Disco/prova41");
		HDDarea.add(5000);
		HDDfiles.add("/Disco/prova/1");
		HDDarea.add(2000);
		HDDfiles.add("/Disco/prova1");
		HDDarea.add(90);
		HDDfiles.add("-1");
		HDDarea.add(60);
		HDDfiles.add("/Disco/pro2");
		HDDarea.add(10);
		HDDfiles.add("/Disco/p1231");
		HDDarea.add(5000);
		HDDfiles.add("/Disco/ciao");
		HDDarea.add(500);
		HDDfiles.add("-1");  //spazio vuoto
		HDDarea.add(500);
		HDDfiles.add("/Disco/william");
		HDDarea.add(5);
		HDDfiles.add("/Disco/user");
		
		//seconda riga
		ArrayList<Integer> HDDarea1 = new ArrayList<Integer>();
		ArrayList<String> HDDfiles1 = new ArrayList<String>();
		HDDarea1.add(2000);
		HDDfiles1.add("/Disco");
		HDDarea1.add(3000);
		HDDfiles1.add("/Disco/prova/1");
		HDDarea1.add(6000);
		HDDfiles1.add("/Disco/prova");
		HDDarea1.add(5000);
		HDDfiles1.add("/Disco");
		
		HDDstate = new Hashtable<Integer, ArrayList<Integer>>();
		HDDstate.put(0, HDDarea1);
		HDDstate.put(1, HDDarea);
		HDDstate.put(2, HDDarea);
	
		HDDlistFiles= new Hashtable<Integer, ArrayList<String>>();
		HDDlistFiles.put(0,HDDfiles1);
		HDDlistFiles.put(1,HDDfiles);
		HDDlistFiles.put(2,HDDfiles);
	
	}
	
	*/
	
	
	public static void resultSimulation(){
		Kernel kernel = DataPlugin.kernel;
		kernel.start_simulation();
		if(kernel.isactiveSimType1()) events=kernel.getResult();
		if(kernel.isactiveSimType2()){
			EventsDisk sim2 = kernel.getResult2();
			HDDevents=sim2.HDDevents;
			HDDstate=sim2.HDDstate;
			HDDlistFiles=sim2.HDDlistFiles;
		}
	}
	
	public static int getMemAddress(int index,int pid, int page){
		int h=-1;
		Hashtable<Integer, ProcessRequest> Mem = events.get(index).getStructureMem();
		for(int i=0;i<Mem.size();i++){
		ProcessRequest p = (ProcessRequest) Mem.get(i);
		if(p.getPID()==pid && p.getSegmento()==page) h=i;
		if(p.getPID()==pid && p.getPagina()==page) h=i;
		}
		return h;
	}

	public static int getSWAPAddress(int index,int pid,int page){
		int h=-1;
		Hashtable<Integer, ProcessRequest> Mem = events.get(index).getStructureSwap();
		for(int i=0;i<Mem.size();i++){
		ProcessRequest p = (ProcessRequest) Mem.get(i);
		if(p.getPID()==pid && p.getSegmento()==page) h=i;
		if(p.getPID()==pid && p.getPagina()==page) h=i;
		}
		return h;
	}
		
	public static  ArrayList<SimEvent> getHDDevents(){
		return HDDevents;
	}
	public static  ArrayList<SimEvent> getTable(){
		return events;
	}


	public static String[] getMemLabels(int index) {
		int NmemC=0;
		if(DataPlugin.blockdata>=1) NmemC=(DataPlugin.memorycentraldata*1024)/DataPlugin.blockdata;
		String[] h=new String[NmemC];
		Hashtable<Integer, ProcessRequest> Mem = events.get(index).getStructureMem();
		for(int i=0;i<NmemC;i++){
			h[i]=" ";
		if(i<Mem.size()){ ProcessRequest p = (ProcessRequest) Mem.get(i);
			h[i]=p.getPID()+"\n";
				if(p.getSegmento()>=0){
					String n=p.getSegmento()+"";
					h[i]+=n;
				}
				else{
					String n=p.getPagina()+"";
					h[i]+=n;
				}
		 	}
		}
	return h;
	}


	public static String[] getSWAPLabels(int index) {
		int NmemS=0;
		if(DataPlugin.blockdata>=1) NmemS=(DataPlugin.swapdata*1024)/DataPlugin.blockdata;
		String[] h=new String[NmemS];
		Hashtable<Integer, ProcessRequest> Mem = events.get(index).getStructureSwap();
		for(int i=0;i<NmemS;i++){
			h[i]=" ";
		if(i<Mem.size()){ ProcessRequest p = (ProcessRequest) Mem.get(i);
			h[i]=p.getPID()+"\n";
				if(p.getSegmento()>0){
					String n=p.getSegmento()+"";
					h[i]+=n;
				}
				else{
					String n=p.getPagina()+"";
					h[i]+=n;
				}
		 	}
		}
	return h;
	}

	public static Hashtable<Integer,ArrayList<Integer>> getHDDstate(){
		return HDDstate;
	}
	
	public static Hashtable<Integer,ArrayList<String>> getHDDlistFiles(){
		return HDDlistFiles;
	}
}
